package pro.cherish.knowledgeruntime.web.dto;

import pro.cherish.knowledgeutil.common.IPUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-03 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：WebApplicationContextDto
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-03 cherish
 */
public class WebApplicationContextDto implements Serializable {

    private static final long serialVersionUID = 4152629744345061961L;

    protected HttpServletRequest request;

    /**
     * 构造方法
     */
    public WebApplicationContextDto() {

    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * 获取请求
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        return this.request;
    }

    /**
     * 获取cookie
     *
     * @return
     */
    public Cookie[] getCookie() {
        return this.request.getCookies();
    }

    /**
     * 获取servletContext
     *
     * @return
     */
    public ServletContext getServletContext() {
        return request.getServletContext();
    }

    /**
     * 获得ContextPath
     *
     * @return Web服务的ContextPath
     */
    public String getContextPath() {
        return request.getContextPath();
    }

    /**
     * 获取IP地址
     *
     * @return
     */
    public String getRemoteAddress() {
        return IPUtils.getIpAddr(request);
    }

    /**
     * 获取主机
     *
     * @return
     */
    public String getRemoteHost() {
        return request.getRemoteHost();
    }

    /**
     * 获取sessionId
     *
     * @return
     */
    public String getSessionId() {
        return request.getRequestedSessionId();
    }

    /**
     * 获取端口
     *
     * @return
     */
    public int getRemotePort() {
        return request.getRemotePort();
    }

    /**
     * 获取URI
     *
     * @return
     */
    public String getRequestURI() {
        return request.getRequestURI();
    }

    /**
     * 获取realPath
     *
     * @return
     */
    public String getRealPath() {
        return request.getSession().getServletContext().getRealPath("/");
    }

    /**
     * 获取requestURL
     *
     * @return
     */
    public String getRequestURL() {
        return request.getRequestURL().toString();
    }

}